//
//  ZZCGreatViewController.m
//  B段项目
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 张智超. All rights reserved.
//

#import "ZZCGreatViewController.h"
#import "ZZCGreatModel.h"
#import "ZZCGreatTableViewCell.h"
#import "ZZCShopViewController.h"
#import "ZZCShopWebViewController.h"

@interface ZZCGreatViewController ()<UITableViewDataSource,UITableViewDelegate,ZZCGreatDeleage>

@property(nonatomic, retain)UITableView *goodTableView;
@property(nonatomic, retain)NSMutableArray *allKeysArray;
@property(nonatomic, assign)BOOL isUpLoad;
@property(nonatomic, assign)NSInteger nextPage;

@end

@implementation ZZCGreatViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleLabel.text = @"良品";
    
    //图标
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH - WIDTH/4.5, WIDTH/40, WIDTH/16, WIDTH/16)];
    img.image = [UIImage imageNamed:@"iconfont-gouwu.png"];
    img.center = CGPointMake(WIDTH - WIDTH/5, self.navigationController.navigationBar.frame.size.height /2);
    [self.rightView addSubview:img];
    
#pragma mark 设置tablaView
    
    self.goodTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.goodTableView.delegate = self;
    self.goodTableView.dataSource = self;
    [self.view addSubview:self.goodTableView];
    //设置滚动条
    self.goodTableView.showsVerticalScrollIndicator = YES;
    
    
#pragma mark 注册
    [self.goodTableView registerClass:[ZZCGreatTableViewCell class] forCellReuseIdentifier:@"goodProCell"];
    
    //初始化
    self.allKeysArray = [[NSMutableArray array] init];
    
    self.isUpLoad = NO;
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(addHeaderRefresh)];
    self.goodTableView.mj_header = header;
    [self.goodTableView.mj_header beginRefreshing];

    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(addFooterRefresh)];
    self.goodTableView.mj_footer = footer;
}

#pragma mark -- 设置下拉刷新
-(void)addHeaderRefresh
{
    self.nextPage = 0;
    self.isUpLoad = NO;//标记为下拉操作
    [self getAFNetWorkingData];
    
}
#pragma mark -- 设置上拉刷新
-(void)addFooterRefresh
{
    self.nextPage += 10;
    self.isUpLoad = YES;
    [self getAFNetWorkingData];
}


#pragma mark- 数据解析
- (void)getAFNetWorkingData
{
    NSString *urlStr = @"http://api2.pianke.me/pub/shop";
    NSString *para = [NSString stringWithFormat:@"auth=&client=1&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&limit=10&start=%ld&version=3.0.6", self.nextPage];
    [ZYTools postUrl:urlStr body:para result:ZYJSON requestStyle:ZYRequestString headerFile:nil success:^(id result) {
        if (self.isUpLoad == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.allKeysArray removeAllObjects];
        }
        NSMutableDictionary *bigDic = [result objectForKey:@"data"];
        NSMutableArray *array = [bigDic objectForKey:@"list"];
        for (NSMutableDictionary *dic in array) {
            ZZCGreatModel *goodsList = [[ZZCGreatModel alloc] init];
            [goodsList setValuesForKeysWithDictionary:dic];
            [self.allKeysArray addObject:goodsList];
        }
        
        [self.goodTableView.mj_header endRefreshing];
        [self.goodTableView.mj_footer endRefreshing];
        [self.goodTableView reloadData];
    } failure:^(NSError *error) {
        
        
    }];
}

#pragma mark - 设置cell数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.allKeysArray.count;
}



#pragma mark - 设置cell 赋值
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZZCGreatTableViewCell *goodProCell = [tableView dequeueReusableCellWithIdentifier:@"goodProCell"];
    
    goodProCell.greatPro = [self.allKeysArray objectAtIndex:indexPath.row];
    goodProCell.goodProDeleage = self;
    goodProCell.selectionStyle = UITableViewCellSelectionStyleNone;
    return goodProCell;
}



#pragma mark - 设置cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return HEIGHT/2;
}


#pragma mark - 触摸cell时触发的方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZZCShopWebViewController *shopVC = [[ZZCShopWebViewController alloc]init];
    ZZCGreatModel *goodModel = [self.allKeysArray objectAtIndex:indexPath.row];
    shopVC.contentId = goodModel.contentid;
    [self.navigationController pushViewController:shopVC animated:YES];
}



#pragma mark - Shop按钮的协议方法
-(void)setDataToController:(NSURL *)url{
    ZZCShopViewController *shopVC = [[ZZCShopViewController alloc]init];
    shopVC.shopUrl = url;
    [self.navigationController pushViewController:shopVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
